<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controlleradmin library
jimport('joomla.application.component.controlleradmin');
 
/**
* Categorys Controller
 */
class ShopControllerAddresses extends JControllerAdmin
{
	/**
	 * Proxy for getModel.
	 * @since	1.6
	 */
	public function getModel($name = 'Addresses', $prefix = 'ShopModel') 
	{
		$model = parent::getModel($name, $prefix, array('ignore_request' => true));
		return $model;
	}

	/**
	* Cancel save or new
	**/
	public function cancel()
	{
		$this->setRedirect('index.php?option=com_shop&view=addresses');
	}
	
	/**
	*	Call published
	*
	***/
	function publish()
	{
		 // Get items to publish from the request.
		$cid    = JRequest::getVar('cid', array(), '', 'array');
		$data    = array('publish' => 1, 'unpublish' => 0, 'archive'=> 2, 'trash' => -2, 'report'=>-3);
		  $task     = $this->getTask();
		  $value    = JArrayHelper::getValue($data, $task, 0, 'int');
		 if (empty($cid)) {
			 JError::raiseWarning(500, JText::_($this->text_prefix.'_NO_ITEM_SELECTED'));
		}
		else
		{
		
				$db = JFactory::getDBO();
				$query = $db->getQuery(true);
				$query->select('*');
				$query->from('#__shop_address');
				$query->where('id = '.intval($cid[0]));
				$db->setQuery((string)$query);
				$row = $db->loadObject();
				
				$query->clear();
				$query->update('#__shop_address');
				$query->set('state = '.$value);
				$query->where('lft >= '.$row->lft);
				$query->where('rgt <= '.$row->rgt);
				$db->setQuery((string)$query);
				//$db->query();
			 // Publish the items.
             if (!$db->query()) {
                  JError::raiseWarning(500, 'COM_SHOP_PUBLISHED_ERROR');
              }
              else {
                  if ($value == 1) {
                      $ntext = $this->text_prefix.'_N_ITEMS_PUBLISHED';
                  }
                  else if ($value == 0) {
                      $ntext = $this->text_prefix.'_N_ITEMS_UNPUBLISHED';
                  }
                  else if ($value == 2) {
                      $ntext = $this->text_prefix.'_N_ITEMS_ARCHIVED';
                  }
                 else {
                     $ntext = $this->text_prefix.'_N_ITEMS_TRASHED';
                  }
                  $this->setMessage(JText::plural($ntext, count($cid)));
             }
		}
		$this->setRedirect('index.php?option=com_shop&view=addresses');
		
	}
	function delete()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));
		// Initialise variables.
		$ids	= JRequest::getVar('cid', array(), '', 'array');
		$ids = implode(',',$ids);
		$db =& JFactory::getDBO();
		//---------- get sub category
		$query = "select * from #__shop_address where parent_id in ( $ids ) ";
		$db->setQuery($query);
		$sub = $db->loadObjectList();
		//check sub exists or not
		if(count($sub) == 0){
			$query = $db->getQuery(true);
			$query->select('*');
			$query->from('#__shop_address');
			$query->where("id in ($ids)");
			$db->setQuery((string)$query);
			$rows = $db->loadObjectList();
			foreach($rows as $row){
				$right = $row->rgt;
						$query->clear();
						$query->update('#__shop_address');
						$query->set('rgt = rgt - 2');
						$query->where('rgt > '.$right );
						$db->setQuery((string)$query);
						$db->query();
						
						$query->clear();
						$query->update('#__shop_address');
						$query->set('lft = lft - 2');
						$query->where('lft > '.$right );
						$db->setQuery((string)$query);
						$db->query();
						
						$query->clear();
						$query->delete('#__shop_address');
						$query->where("id = $row->id");
						$db->setQuery($query);
						$db->query();
			}
			$this->setMessage(JText::_('COM_SHOP_ADDRESS_DELETE_SUCCESS').$ids);
			$this->setRedirect('index.php?option=com_shop&view=addresses');
		}
		else{
			JError::raiseWarning(500, JText::_('COM_SHOP_ADDRESS_EXISTS_SUB_ADDRESS'));
			$this->setRedirect('index.php?option=com_shop&view=addresses');
		}
	}
	
	/**
	*
	*	Save order
	***/	
	function saveorder()
	{
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
		$cid    = JRequest::getVar('cid', array(), '', 'array');
		$order    = JRequest::getVar('order', array(), '', 'array');
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		
		//----------
		/* JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
		$row = JTable::getInstance('Address','ShopTable');
		$row->reorder(); */
		
		foreach($cid as $i=>$row){
			$query->clear();
			$query->update("#__shop_address");
			$query->set('ordering = '.$order[$i]);
			$query->where('id = '.$row);
			$db->setQuery((string)$query);
			$db->query();
		}
		
		$this->setMessage(JText::_('COM_SHOP_ADDRESS_ORDER_SUCCESS'));
		$this->setRedirect('index.php?option=com_shop&view=addresses');
	}
	
	/*
	* Order down address
	*
	*/
	function reorder()
	{
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
		$cid    = JRequest::getVar('cid', array(), '', 'array');
		$data    = array('orderdown' => 1, 'orderup' => -1);
		$task     = $this->getTask();
		$value    = JArrayHelper::getValue($data, $task, 0, 'int');
		if (empty($cid)) {
			 JError::raiseWarning(500, JText::_($this->text_prefix.'_NO_ITEM_SELECTED'));
		}else{
			$row = JTable::getInstance('Address','ShopTable');
			$row->load((int)$cid[0]);
			$row->move($value);
			$this->setMessage(JText::_('COM_SHOP_ADDRESS_ORDER_SUCCESS'));
			$this->setRedirect('index.php?option=com_shop&view=addresses');
		}
	}
	function active()
	{
		$cid = JRequest::getVar('cid');
		$id = intval($cid[0]);
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->update('#__shop_address');
		$query->set('active = 1');
		$query->where('id = '.$id );
		$db->setQuery((string)$query);
		$db->query();
		$this->setMessage(JText::_('COM_SHOP_ADDRESS_ACTIVE_SUCCESS'));
		$this->setRedirect('index.php?option=com_shop&view=addresses');
	}
	function unactive()
	{
		$cid = JRequest::getVar('cid');
		$id = intval($cid[0]);
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->update('#__shop_address');
		$query->set('active = 0');
		$query->where('id = '.$id );
		$db->setQuery((string)$query);
		$db->query();
		$this->setMessage(JText::_('COM_SHOP_ADDRESS_ACTIVE_DISABLE_SUCCESS'));
		$this->setRedirect('index.php?option=com_shop&view=addresses');
	}
}